﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Text;

namespace WuxiWTO.Web
{
    public partial class Default : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.leftModeArt.Text = GetLeftContent();
                BindImageNews();
                BindRightTopArticle();
                this.litRightMiddleMode.Text = GetRightMiddelContent();
                this.litRightBottomLeft.Text = GetRightBottomLeftContent();
                this.litRightBottomRight.Text = GetRightBottomRightContent();
                GetTopCompanies();
                SetTheLoginArea();
            }
        }

        //private void GetComplain()
        //{
        //    List<Complaint> list = ctx.Complaints.OrderByDescending(p => p.update_date).Where(p=>p.display_toppage == '1').Take(6).ToList();
        //    this.rptComplain.DataSource = list;
        //    this.rptComplain.DataBind();
        //}
        private void BindImageNews()
        {
            var list = ctx.image_news.OrderByDescending(p => p.update_date).Take(6).ToList();
            this.rptImageNews.DataSource = list;
            this.rptImageNews.DataBind();
        }

        private void BindRightTopArticle()
        {
            category cat = ctx.categories.OrderBy(p => p.sort_num).SingleOrDefault(p => p.position == "RT");
            int pid = cat.art_parent_kind.Value;
            int cid = cat.art_kind_id;

            litRTtitle.Text = cat.alias == "" ? cat.art_kind_name : cat.alias;
            hyMoreRT.NavigateUrl = "articleList.aspx?pid=" + pid + "&cid=" + cid;

            List<article> list = ctx.articles.OrderByDescending(p => p.update_date).Where(p => p.parent_category_id == pid && p.category_id == cid).Take(7).ToList();
            this.rptDynamic.DataSource = list;
            this.rptDynamic.DataBind();
        }

        private void GetTopCompanies()
        {
            List<top_company> list = (from c in ctx.top_companies
                                  orderby c.ID descending
                                  select c).ToList();
            this.rptTopCompany.DataSource = list;
            this.rptTopCompany.DataBind();
        }

        private string GetLeftContent()
        {
            return CategoryInfo.GetLeftMode();
        }

        private string GetRightMiddelContent()
        {
            return CategoryInfo.GetRightMiddleMode();
        }

        private string GetRightBottomLeftContent()
        {
            return CategoryInfo.GetRightBottomLeftMode();
        }

        private string GetRightBottomRightContent()
        {
            return CategoryInfo.GetRightBottomMode();
        }

        protected void rptYoungImage_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                image_new image = e.Item.DataItem as image_new;
                HyperLink hyLink = e.Item.FindControl("hyLink") as HyperLink;
                Image imgLink = e.Item.FindControl("imgAD") as Image;
                hyLink.ToolTip = image.news_title;
                imgLink.ImageUrl = image.image_path;
                hyLink.NavigateUrl = "commonNews.aspx?id=" + image.ID;
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.txtUser.Text.Trim() == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "idf", "alert('请填写用户名');", true);
                return;
            }
            if (this.txtPassword.Text.Trim() == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "pasf", "alert('请填写密码');", true);
                return;
            }
            var user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == this.txtUser.Text.Trim() && p.supporter_password == this.txtPassword.Text.Trim());
            if (user == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "idf", "alert('用户名或者密码错误，请重新输入');", true);
                return;
            }
            else
            {
                HttpCookie cookie = new HttpCookie("wtouser");
                cookie.Values["name"] = this.txtUser.Text.Trim();
                cookie.Values["type"] = user.supporter_type.ToString();
                cookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(cookie);

                Session["user_name"] = this.txtUser.Text.Trim();
                Session["user_type"] = user.supporter_type;
                Session["user_id"] = user.supporter_id;

                if (ViewState["prePage"] != null)
                {
                    Response.Redirect(ViewState["prePage"].ToString());
                }
                else
                {
                    Response.Redirect("userCenter.aspx");
                }
            }
        }

        #region 页面顶部登录模块

        private void SetTheLoginArea()
        {
            //session不为空，已经登录
            if (Session["user_name"] != null)
            {
                string uname = Session["user_name"].ToString();
                var user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == uname);
                if (user != null)
                {
                    loginPanel.Visible = false;
                    msgPanel.Visible = true;
                    if (user.supporter_type == 'I')
                    {
                        loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.supporter_name);
                    }
                    else
                    {
                        loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.enterprise_name);
                    }
                }
                else
                {
                    loginPanel.Visible = true;
                    msgPanel.Visible = false;
                }
            }
            //session为空，查看cooki有没有
            else
            {
                if (Request.Cookies["wtouser"] == null)
                {
                    loginPanel.Visible = true;
                    msgPanel.Visible = false;
                }
                else
                {
                    string uname = Server.HtmlEncode(Request.Cookies["wtouser"]["name"].ToString());
                    var user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == uname);
                    if (user != null)
                    {
                        loginPanel.Visible = false;
                        msgPanel.Visible = true;
                        if (user.supporter_type == 'I')
                        {
                            loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.supporter_name);
                        }
                        else
                        {
                            loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.enterprise_name);
                        }
                        Session["user_name"] = user.supporter_login_name;
                        Session["user_type"] = user.supporter_type;
                        Session["user_id"] = user.supporter_id;
                    }
                    else
                    {
                        loginPanel.Visible = true;
                        msgPanel.Visible = false;
                    }

                }
            }
        }
        #endregion
    }
}